﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FaymanController.Utils;
using Newtonsoft.Json;

namespace FaymanController
{
    public partial class Main_Form : Form
    {
        public Main_Form()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (numericUpDown1.Value < 0 || numericUpDown1.Value >= 24
                || numericUpDown2.Value < 0 || numericUpDown2.Value > 59
                || numericUpDown3.Value < 0 || numericUpDown3.Value >= 24
                || numericUpDown4.Value < 0 || numericUpDown4.Value > 59
                || numericUpDown1.Value > numericUpDown3.Value
                || (numericUpDown1.Value == numericUpDown3.Value &&
                    numericUpDown2.Value >= numericUpDown4.Value)
                )
            {
                MessageBox.Show("时间范围有误！", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var a = numericUpDown1.Value; var b = numericUpDown2.Value;
            var c = numericUpDown3.Value; var d = numericUpDown4.Value;
            GlobalData.TimeTable.Add(new TimeData(
                  (a >= 10 ? a.ToString() : ("0" + a.ToString())) + ":"
                + (b >= 10 ? b.ToString() : ("0" + b.ToString())), //StartTime
                  (c >= 10 ? c.ToString() : ("0" + c.ToString())) + ":"
                + (d >= 10 ? d.ToString() : ("0" + d.ToString())), //EndTime
                !radioButton1.Checked));
            RefreshGrid();
        }

        private void RefreshGrid()
        {
            listView1.Items.Clear();
            listView1.BeginUpdate();
            foreach (TimeData t in GlobalData.TimeTable)
            {
                ListViewItem item = new ListViewItem();
                item.Text = t.StartTime + " - " + t.EndTime;
                item.SubItems.Add(t.CanPlay ? "是" : "否");
                listView1.Items.Add(item);
            }
            listView1.EndUpdate();
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("你没有选中任何内容！", "Error", 
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            GlobalData.TimeTable.RemoveAt(listView1.SelectedItems[0].Index);
            RefreshGrid();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Config config = new Config();
            config.otherTimeCanPlay = (radioButton4.Checked ? false : true);
            config.allowExtra = checkBox1.Checked;
            config.extraMin = (int)numericUpDown5.Value;
            config.extraSec = (int)numericUpDown6.Value;
            config.extraTimeType = radioButton9.Checked ? 0 : 1;
            config.notifyContent = textBox1.Text;
            config.stopType = radioButton5.Checked ? 0 : (radioButton6.Checked ? 1 : 
                            (radioButton7.Checked ? 2 : (radioButton8.Checked ? 3 : -1)));

            GlobalData.GlobalConfig = config; //更新全局配置文件

            if (ConfigLoader.SaveConfig(Application.StartupPath+@"\conf.json",config) &&
                ConfigLoader.SaveTimeTable(Application.StartupPath + @"\table.json",GlobalData.TimeTable))
            {
                MessageBox.Show("保存配置文件成功！", "Done",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void Main_Form_Load(object sender, EventArgs e)
        {
            if (GlobalData.GlobalConfig != null)
            {
                LoadConfig();
            }
        }

        private void LoadConfig()
        {
            /// read config
            if (!GlobalData.GlobalConfig.otherTimeCanPlay)
            {
                radioButton4.Checked = true; radioButton5.Checked = false;
            }
            else
            {
                radioButton4.Checked = false; radioButton5.Checked = true;
            }
            checkBox1.Checked = GlobalData.GlobalConfig.allowExtra;
            numericUpDown5.Value = GlobalData.GlobalConfig.extraMin;
            numericUpDown6.Value = GlobalData.GlobalConfig.extraSec;
            if (GlobalData.GlobalConfig.extraTimeType == 0)
            {
                radioButton9.Checked = true; radioButton10.Checked = false;
            }
            else
            {
                radioButton9.Checked = false; radioButton10.Checked = true;
            }
            textBox1.Text = GlobalData.GlobalConfig.notifyContent;
            radioButton5.Checked = radioButton6.Checked
                = radioButton7.Checked = radioButton8.Checked = false;
            switch (GlobalData.GlobalConfig.stopType)
            {
                case 0:
                    radioButton5.Checked = true;
                    break;
                case 1:
                    radioButton6.Checked = true;
                    break;
                case 2:
                    radioButton7.Checked = true;
                    break;
                case 3:
                    radioButton8.Checked = true;
                    break;
                default:
                    break;
            }
            /// read time table
            RefreshGrid();
        }
    }
}
